package com.system.core.plugins.mybatis.custom;

import java.util.List;
import java.util.function.Predicate;

import org.apache.commons.lang3.StringUtils;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

import lombok.experimental.UtilityClass;

@UtilityClass
public class MyInsertXML {

	public void insertIgnoreSelectiveXML(Document document, IntrospectedTable introspectedTable) {
		
	}
	
	public void insertIgnoreBatchXML(Document document, IntrospectedTable introspectedTable) {
		String keyName= "";
		if(introspectedTable.getPrimaryKeyColumns().size()>0)
			keyName = introspectedTable.getPrimaryKeyColumns().get(0).getJavaProperty();
		XmlElement batchInsertEle = new XmlElement("insert");
		batchInsertEle.addAttribute(new Attribute("id", "insertIgnoreBatch"));
		batchInsertEle.addAttribute(new Attribute("parameterType", "java.util.List"));
		batchInsertEle.addAttribute(new Attribute("useGeneratedKeys", "true"));
		if(StringUtils.isNotBlank(keyName))
			batchInsertEle.addAttribute(new Attribute("keyProperty", keyName));
		TextElement zhushi = new TextElement("<!--\r\n"
				+ "      WARNING - @mbg.generated\r\n"
				+ "      This element is automatically generated by MyBatis Generator, do not modify.\r\n"
				+ "    -->");
		StringBuilder insertsql = new StringBuilder("insert ignore into ");
		insertsql.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()+" (");
		int i = 1;
		List<IntrospectedColumn> columns = introspectedTable.getAllColumns();
		columns.removeIf(new Predicate<IntrospectedColumn>() {
			
			@Override
			public boolean test(IntrospectedColumn column) {
				if(column.isIdentity() 
						|| column.getActualColumnName().equalsIgnoreCase("create_time")
						|| column.getActualColumnName().equalsIgnoreCase("update_time"))
					return true;
				else
					return false;
			}
			
		});
		for(IntrospectedColumn column:columns) {
			if(column.isIdentity() 
					|| column.getActualColumnName().equalsIgnoreCase("create_time")
					|| column.getActualColumnName().equalsIgnoreCase("update_time"))
				continue;
			insertsql.append(column.getActualColumnName());
			if(i < columns.size()){
				insertsql.append(",");
			}
			if(i%2==0) {
				insertsql.append("\n\t\t");
			}
			i++;
		}
		insertsql.append(") VALUES");
		TextElement insert1 = new TextElement(insertsql.toString());
		batchInsertEle.addElement(zhushi);
		batchInsertEle.addElement(insert1);
		
		XmlElement foreachEle = new XmlElement("foreach");
		foreachEle.addAttribute(new Attribute("collection", "list"));
		foreachEle.addAttribute(new Attribute("item", "item"));
		foreachEle.addAttribute(new Attribute("separator", ","));
		
		batchInsertEle.addElement(foreachEle);
		StringBuilder valuesql = new StringBuilder("(");
		i = 1;
		for(IntrospectedColumn column:columns) {
			valuesql.append("#{item."+column.getJavaProperty()+",jdbcType="+column.getJdbcTypeName()+"}");
			if(i<columns.size()){
				valuesql.append(",");
			}
			if(i%2 == 0) {
				valuesql.append("\n\t\t");
			}
			i++;
		}
		valuesql.append(")");
		TextElement insert2 = new TextElement(valuesql.toString());
		foreachEle.addElement(insert2);
		document.getRootElement().addElement(batchInsertEle);
	}
	public void insertBatchXML(Document document, IntrospectedTable introspectedTable) {
		String keyName= "";
		if(introspectedTable.getPrimaryKeyColumns().size()>0)
			keyName = introspectedTable.getPrimaryKeyColumns().get(0).getJavaProperty();
		XmlElement batchInsertEle = new XmlElement("insert");
		batchInsertEle.addAttribute(new Attribute("id", "insertBatch"));
		batchInsertEle.addAttribute(new Attribute("parameterType", "java.util.List"));
		batchInsertEle.addAttribute(new Attribute("useGeneratedKeys", "true"));
		if(StringUtils.isNotBlank(keyName))
			batchInsertEle.addAttribute(new Attribute("keyProperty", keyName));
		TextElement zhushi = new TextElement("<!--\r\n"
				+ "      WARNING - @mbg.generated\r\n"
				+ "      This element is automatically generated by MyBatis Generator, do not modify.\r\n"
				+ "    -->");
		StringBuilder insertsql = new StringBuilder("insert into ");
		insertsql.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()+" (");
		int i = 1;
		List<IntrospectedColumn> columns = introspectedTable.getAllColumns();
		columns.removeIf(new Predicate<IntrospectedColumn>() {

			@Override
			public boolean test(IntrospectedColumn column) {
				if(column.isIdentity() 
						|| column.getActualColumnName().equalsIgnoreCase("create_time")
						|| column.getActualColumnName().equalsIgnoreCase("update_time"))
					return true;
				else
					return false;
			}
			
		});
		for(IntrospectedColumn column:columns) {
			if(column.isIdentity() 
					|| column.getActualColumnName().equalsIgnoreCase("create_time")
					|| column.getActualColumnName().equalsIgnoreCase("update_time"))
				continue;
			insertsql.append(column.getActualColumnName());
			if(i < columns.size()){
				insertsql.append(",");
			}
			if(i%2==0) {
				insertsql.append("\n\t\t");
			}
			i++;
		}
		insertsql.append(") VALUES");
		TextElement insert1 = new TextElement(insertsql.toString());
		batchInsertEle.addElement(zhushi);
		batchInsertEle.addElement(insert1);
		
		XmlElement foreachEle = new XmlElement("foreach");
		foreachEle.addAttribute(new Attribute("collection", "list"));
		foreachEle.addAttribute(new Attribute("item", "item"));
		foreachEle.addAttribute(new Attribute("separator", ","));
		
		batchInsertEle.addElement(foreachEle);
		StringBuilder valuesql = new StringBuilder("(");
		i = 1;
		for(IntrospectedColumn column:columns) {
			valuesql.append("#{item."+column.getJavaProperty()+",jdbcType="+column.getJdbcTypeName()+"}");
			if(i<columns.size()){
				valuesql.append(",");
			}
			if(i%2 == 0) {
				valuesql.append("\n\t\t");
			}
			i++;
		}
		valuesql.append(")");
		TextElement insert2 = new TextElement(valuesql.toString());
		foreachEle.addElement(insert2);
		document.getRootElement().addElement(batchInsertEle);
	}
}
